function Share(){
    var me = this;

    this.from = clientCollect.getFromMark();

    clientCollect.linkChange = function (link) {
        return me.format(link);
    };
    clientCollect.sendShare = function(type, data) {
        data = extend(me.report(), data || {})
        data.to_mark = type;
        clientCollect.send("event", data, "share");
    };
}

var SharePrototype = Share.prototype;
SharePrototype.name = "share";
SharePrototype.report = function(){
    var linkArr = window.location.href.split("?");
    var linkQ = getQuery(null, linkArr[1] || "?");
    var from = linkQ.pi_sharer_id || '';
    return {
        "log_type": "event"
        ,"category": "behavior"
        ,"action": "share"
        ,"sharer_id": from
        ,"title": encodeURIComponent(document.title)
        // 页面地址
        ,"url": encodeURIComponent(window.location.href)
    };
}

/**
 * 格式化分享链接
 * @param  {String} link 原始分享链接
 * @return {String}      处理后的链接
 */
SharePrototype.format = function(link) {
    var linkArr = link.split("?");
    var linkQ = getQuery(null, linkArr[1] || "?");

    var q = getQuery();
    if (link.indexOf("?") === -1) {
        link += "?";
    }

    if (linkQ.pi_sharer_id) {
        link = link.replace(/pi_sharer_id=[a-zA-Z0-9-\-]+/, "pi_sharer_id=" + clientCollect.uuid);
    } else {
        link += "&pi_sharer_id=" + clientCollect.uuid;
    }

    if (this.from) {
        if (linkQ.pi_from_mark) {
            link = link.replace(/pi_from_mark=[a-zA-Z0-9-\-]+/, "pi_from_mark=" + this.from);
        } else {
            link += "&pi_from_mark=" + this.from;
        }
    }
    var ts = (new Date).getTime();
    if (linkQ.pi_from_mark) {
        link = link.replace(/pi_ts=\d+/, "pi_ts=" + ts);
    } else {
        link += "&pi_ts=" + ts;
    }

    return link;
}

clientCollect.add(Share);
